package com.asj.house.service.impl;

import com.asj.house.entity.HouseRoom;
import com.asj.house.entity.HouseRoomDetail;
import com.asj.house.mapper.HouseRoomDetailMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.returns.RespTable;
import com.asj.house.service.HouseRoomDetailService;
import com.asj.house.service.HouseRoomService;
import com.asj.house.vo.MapVO;
import com.asj.house.vo.param.RoomParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 * 房源详细信息表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-16
 */
@Service
public class HouseRoomDetailServiceImpl extends ServiceImpl<HouseRoomDetailMapper, HouseRoomDetail> implements HouseRoomDetailService {

    @Autowired
    private HouseRoomService houseRoomService;

    @Override
    public RespTable<List<HouseRoomDetail>> listHouseRoomDetail(RoomParam roomParam) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(roomParam.getName())) {
            queryWrapper.lambda().like(HouseRoom::getName, roomParam.getName());
        }
        queryWrapper.lambda().orderByDesc(HouseRoom::getId);
        IPage<HouseRoom> iPage = new Page<>(roomParam.getPage(), roomParam.getLimit());
        IPage<HouseRoom> page = houseRoomService.page(iPage, queryWrapper);

        if (page.getRecords().isEmpty()) {
            return new RespTable<>(RespCode.SUCCESS, new ArrayList<>(), page.getTotal());
        }

        List<HouseRoomDetail> houseRoomDetailList = Lists.newArrayList();
        for (HouseRoom houseRoom : page.getRecords()) {
            QueryWrapper<HouseRoomDetail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.lambda().eq(HouseRoomDetail::getRoomId, houseRoom.getId());
            HouseRoomDetail houseRoomDetail = baseMapper.selectOne(detailQueryWrapper);
            houseRoomDetail.setRoomName(houseRoom.getName());
            houseRoomDetailList.add(houseRoomDetail);
        }

        houseRoomDetailList = houseRoomDetailList.stream()
                .sorted(Comparator.comparing(HouseRoomDetail::getRoomId).reversed())
                .collect(Collectors.toList());

        return new RespTable<>(RespCode.OK, houseRoomDetailList, page.getTotal());
    }

    @Override
    public RespEntity updateHouseRoomDetail(HouseRoomDetail houseRoomDetail) {
        if (StringUtils.isNotBlank(houseRoomDetail.getProvince())) {
            if (checkProvince(houseRoomDetail.getProvince())) {
                //是省份
                String[] split = houseRoomDetail.getProvince().split("省");
                String province = split[0];
                if (StringUtils.isNotBlank(province)) {
                    houseRoomDetail.setProvince(province);
                }
            }else {
                //是直辖市
                String[] split = houseRoomDetail.getProvince().split("市");
                String province = split[0];
                if (StringUtils.isNotBlank(province)) {
                    houseRoomDetail.setProvince(province);
                }
            }

        }
        baseMapper.updateById(houseRoomDetail);
        return new RespEntity(RespCode.SUCCESS);
    }

    /**
     * 直辖市校验
     * @param province
     * @return
     */
    private boolean checkProvince(String province) {
        boolean flag = true;
        switch (province) {
            case "北京市" : flag = false; break;
            case "天津市" : flag = false; break;
            case "上海市" : flag = false; break;
            case "重庆市" : flag = false;
        }
        return flag;
    }

    @Override
    public RespEntity<List<MapVO>> chinaMap() {
        List<MapVO> mapVOS = baseMapper.chinaMap();
        return new RespEntity<>(RespCode.SUCCESS, mapVOS);
    }

    @Override
    public RespEntity<List<MapVO>> provinceMap(String province) {
        List<MapVO> mapVOS = baseMapper.provinceMap(province);
        return new RespEntity<>(RespCode.SUCCESS, mapVOS);
    }
}
